package RestaurantManagement;
import java.util.ArrayList;
import java.util.HashMap;

import RestaurantManagement.oop.demo.restaurantmanagementsystem.Ingredient;
import RestaurantManagement.oop.demo.restaurantmanagementsystem.Meal;
import RestaurantManagement.oop.demo.restaurantmanagementsystem.RestaurantBilling;


/**
 * Service class for managing restaurant operations including ingredients,
 * meals, orders, and billing.
 */
public class RestaurantService {
    private RestaurantBilling billing ;
    private HashMap<String, Ingredient> ingredientInventory;

    public RestaurantService() {
        this.billing = new RestaurantBilling();
        this.ingredientInventory = new HashMap<>();
    }

    /**
     * Adds a new ingredient to the restaurant's inventory.
     *
     * @param name  the name of the ingredient to add
     * @param price the price of the ingredient (must be positive)
     * @throws IllegalArgumentException if price is negative
     */
    public void addIngredient(String name, double price) {
        if (price < 0) {
            throw new IllegalArgumentException("Price must be positive");
        }
        for (Ingredient ingredient : ingredientInventory.values()) {
            if (ingredient.getName().equals(name)) {
                throw new IllegalArgumentException("Ingredient already exists: " + name);
            }
        }
        ingredientInventory.put(name, new Ingredient(name, price));
    }

    /**
     * Creates a new meal with the specified ingredients and adds it to the menu.
     *
     * @param mealName        the name of the meal to create
     * @param ingredientNames list of ingredient names to include in the meal
     * @throws IllegalArgumentException if any ingredient is not found in inventory
     */
    public void createMeal(String mealName, ArrayList<String> ingredientNames) {
        Meal meal = new Meal(mealName);
        for (String ingredientName : ingredientNames) {
            Ingredient ingredient = ingredientInventory.get(ingredientName);
            if (ingredient == null) {
                throw new IllegalArgumentException("Ingredient not found: " + ingredientName);
            }
            meal.addIngredient(ingredient);
        }
        billing.addMeal(meal);
    }

    public boolean removeMeal(String mealName)
    {
        return billing.removeMeal(mealName);
    }

    /**
     * Places an order with specified quantities of meals.
     *
     * @param mealQuantities map of meal names to quantities to order
     * @return a unique order ID for tracking purposes
     * @throws IllegalArgumentException if any meal is not found in the menu
     */
    public String placeOrder(HashMap<String, Integer> mealQuantities)
    {
        for (String mealName : mealQuantities.keySet()) {
            int quantity = mealQuantities.get(mealName);
            for (int i = 0; i < quantity; i++) {
                if (!billing.addMealToOrder(mealName)) {
                    throw new IllegalArgumentException("Meal not found: " + mealName);
                }
            }
        }
        return "ORD-" + System.currentTimeMillis();
    }

    /**
     * Calculates the total bill amount for an order.
     *
     * @param orderId the ID of the order to calculate (currently unused)
     * @return the total amount due for the order
     */

    public double calculateBill(String orderId)
    {
        return billing.calculateBill();
    }

    public boolean findMealByName(String mealName) {
        return billing.findMealByName(mealName) != null;
    }

    public String getAllIngredients() {
        if (ingredientInventory.isEmpty()) {
            return "No ingredients in inventory";
        }
        StringBuilder sb = new StringBuilder();
        for (Ingredient ingredient : ingredientInventory.values()) {
            sb.append(ingredient.toString()).append("\n");
        }
        return sb.toString().trim();
    }
}


